
// Figure D6

/* =============================================================================
   Step 1: Run yearly regressions
============================================================================= */

use "${dataout}MainDataset" , clear

** Select variables to loop over: 
local yearlist  2017 2016 2015 2014 2013 2012 2011 2010 2009 2008
local outcomelist BEV_fam_yes ICE_fam_count   
local treatvar1list toll_fam_mean_KPI
local treatvar2list ptl_fam_km_mean       

** Regressions - loop over: outcome, treatment, year
foreach y in `yearlist' {
	foreach outcome in `outcomelist'{
		foreach treatvar1 in `treatvar1list'{
			foreach treatvar2 in `treatvar2list'{
				* Couples
				reghdfe `outcome' `treatvar1'  `treatvar2'  $age    $distance  $time  $publictime  $publicquality  ///
					if year==`y' & couple == 1, ///
					absorb($FE   $household  $income  $employment  $education   ) vce(cluster $clustervar) 
				summarize `outcome' if e(sample)==1
				estadd scalar MeanDep = r(mean), replace
				summarize `treatvar1' if e(sample)==1
				estadd scalar Treat1 = r(mean), replace
				summarize `treatvar2' if e(sample)==1
				estadd scalar Treat2 = r(mean), replace
				estadd local year="`y'" , replace
				estadd local grkFE="\checkmark", replace
				estadd local grkbFE="\checkmark", replace
				estadd local HHcon="\checkmark", replace
				estadd local HHWork="\checkmark", replace
				estadd local PublicTime="\checkmark", replace
				estadd local grkFE_W="Yes", replace
				estadd local grkbFE_W="Yes", replace
				estadd local HHcon_W="Yes", replace
				estadd local HHWork_W="Yes", replace
				estadd local PublicTime_W="Yes", replace
				eststo reg_d4_`outcome'_`y' 
				* Save to .ster
				estimates save "${ster}FigD6_`y'_couple_`outcome'" , replace
				
				* Singles
				reghdfe `outcome' `treatvar1'  `treatvar2'  $ageS    $distanceS  $timeS  $publictimeS  $publicqualityS  ///
					if year==`y' & couple == 0, ///
					absorb($FES   $householdS  $incomeS  $employmentS  $educationS   ) vce(cluster $clustervarS) 
				summarize `outcome' if e(sample)==1
				estadd scalar MeanDep = r(mean), replace
				summarize `treatvar1' if e(sample)==1
				estadd scalar Treat1 = r(mean), replace
				summarize `treatvar2' if e(sample)==1
				estadd scalar Treat2 = r(mean), replace
				estadd local year="`y'" , replace
				estadd local grkFE="\checkmark", replace
				estadd local grkbFE="\checkmark", replace
				estadd local HHcon="\checkmark", replace
				estadd local HHWork="\checkmark", replace
				estadd local PublicTime="\checkmark", replace
				estadd local grkFE_W="Yes", replace
				estadd local grkbFE_W="Yes", replace
				estadd local HHcon_W="Yes", replace
				estadd local HHWork_W="Yes", replace
				estadd local PublicTime_W="Yes", replace
				eststo reg_d4_`outcome'_`y' 
				* Save to .ster
				estimates save "${ster}FigD6_`y'_single_`outcome'" , replace
			}
		}
	}
}

/* =============================================================================
   Step 1: Make tables from .ster files
============================================================================= */

local outcomelist  BEV_fam_yes   ICE_fam_count 
local treatvar1list toll_fam_mean   
local treatvar2list   ptl_fam_km_mean    
local yearlist  2017 2016 2015 2014 2013 2012 2011 2010 2009 2008 


foreach outcome in `outcomelist'{
	foreach treatvar1 in `treatvar1list'{
		foreach treatvar2 in `treatvar2list'{

			capt estimates drop *

			foreach y in `yearlist' {
				
				estimates use "${ster}FigD6_`y'_couple_`outcome'" 
				estimates store c`y'

				estimates use "${ster}FigD6_`y'_single_`outcome'" 
				estimates store s`y'
			}
			
			if "`outcome'"=="BEV_fam_yes" {
				local ytitle "BEV (yes=1)"
			}
			if "`outcome'"=="ICE_fam_count" {
				local ytitle "Number of ICEVs"
			}
			if "`outcome'"=="car_fam_count" {
				local ytitle "Number of Cars"
			}
			if "`outcome'"=="car_fam_yes" {
				local ytitle "Car (yes=1)"
			}
			else 
			
			local coefflist `treatvar1'  `treatvar2'
			foreach coeff in `coefflist'{
				coefplot ///
				   (c2008, label(Couples) msymbol(O) ) (s2008, label(Singles) msymbol(S)), bylabel(2008)  ///
				|| c2009 s2009, bylabel(2009)  ///
				|| c2010 s2010, bylabel(2010)  ///
				|| c2011 s2011, bylabel(2011)  ///
				|| c2012 s2012, bylabel(2012)  ///
				|| c2013 s2013, bylabel(2013)  ///
				|| c2014 s2014, bylabel(2014)  ///
				|| c2015 s2015, bylabel(2015)  ///
				|| c2016 s2016, bylabel(2016)  ///
				|| c2017 s2017, bylabel(2017)  ///
				|| ///
				, keep(`coeff') bycoef  byopts(yrescale) vertical  ///
				yline(0 , lcolor(gs6)  lpattern(dash))  ytitle(`ytitle') /*ytitle(`: var label `outcome'')*/ ///
				/*title(`: var label `coeff'')*/  graphregion(color(white))  ///
				scheme() scale(1.25) plotregion(lcolor(black)) msize(medium)  yscale(titlegap(1.5)) ///
				name(figD6`outcome'`coef', replace)

				
				graph export     "${figures}FigureD6_`outcome'_`coeff'.png" , replace
				graph export     "${figures}FigureD6_`outcome'_`coeff'.pdf" , replace
				graph save       "${figures}FigureD6_`outcome'_`coeff'.gph", replace
			}
		}
	}
}
